package com.atguigu.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author tongatong
 * @name CustomThread
 * @description
 * 问题描述：
 * 多线程之间按顺序调用，实现A->B->C
 * 三个线程启动，要求如下：
 *
 * AA打印5次，BB打印10次，CC打印15次
 * 接着
 * AA打印5次，BB打印10次，CC打印15次
 * ......三个线程来10轮
 *
 * 知识点：
 *   精确调用某个线程
 * 口诀：
 *   1. 线程 操作 资源类
 *   2. 判断 操作 唤醒
 *   3. 虚假唤醒
 *   4. 标志的修改，更新操作（本期重点）
 *
 * @date 2021/9/1
 * @time 20:07
 */
public class CustomThread {
    public static void main(String[] args) {
        MyResource myResource = new MyResource();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myResource.print5();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "AA").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myResource.print10();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "BB").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myResource.print15();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "CC").start();

    }
}

//资源类
class MyResource {
    private int flag = 1;
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();


    public void print5() throws InterruptedException {
        lock.lock();
        try {
            // access the resource protected by this lock
            //判断  虚假唤醒
            while (flag != 1)
                condition1.await();
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            //更新flag
            flag = 2;
            //唤醒
            condition2.signal();
        } finally {
            lock.unlock();
        }

    }
    public void print10() throws InterruptedException {
        lock.lock();
        try {
            // access the resource protected by this lock
            while (flag != 2)
                condition2.await();
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            flag = 3;
            condition3.signal();
        } finally {
            lock.unlock();
        }

    }
    public void print15() throws InterruptedException {
        lock.lock();
        try {
            // access the resource protected by this lock
            while (flag != 3)
                condition3.await();
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName()+"\t"+i);
            }
            flag = 1;
            condition1.signal();
        } finally {
            lock.unlock();
        }

    }

}